package com.yumei.trader.gym.dbase;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class DBaseManager {

    private static DBaseManager instance;

    private SharedPreferences appPrefernce;

    private HashMap<String,String> paramHash = new HashMap<>();

    private DBaseManager(Context context){

        appPrefernce = context.getSharedPreferences(DBaseConst.APP_SHARE_NAME, Activity.MODE_PRIVATE);
    }

    public static DBaseManager getInstance(Context context){

        if(instance == null){
            instance = new DBaseManager(context);
        }
        return instance;
    }

    public void addParam(String key, String value){
        paramHash.put(key,value);
    }

    public void save(){

        SharedPreferences.Editor editor = appPrefernce.edit();

        Iterator<Map.Entry<String,String>> iterator = paramHash.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String,String> entry = iterator.next();
            String key = entry.getKey();
            String value = entry.getValue();
            editor.putString(key, value);
            System.out.println("put="+key+","+value);
        }

        if(editor.commit()){
            System.out.println(" editor.commit()  true ");
            paramHash.clear();
        }else {
            System.out.println(" editor.commit()  false ");
        }
    }

    public String getValue(String key, String defStr){
        return appPrefernce.getString(key,defStr);
    }

    public String getValue(String key){
        return appPrefernce.getString(key,"");
    }
}
